<%@ page import="javax.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="forms.StudentForm" %>
<%@ page import="forms.DegreeForm" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
	<head>
		<script type="text/javascript">
			function showApp(form) {
				
				var app_button_id = form.elements[0].value;
				
				if (document.getElementById(app_button_id).value == "Get Application") {
					// Construct XHR object
					var xmlHttp=new XMLHttpRequest();
	
					// Construct URL with form values as parameters
					var url="../showapp.do?" + form.elements[0].name + 
								"=" + escape(form.elements[0].value);
	
					// Construct XHR callback function
					xmlHttp.onreadystatechange=function() {
						
						// Check if the XHR response is ready
						if (xmlHttp.readyState != 4) return;
	
						// Check if the server returned the response with no errors
						if (xmlHttp.status != 200) {
							alert("HTTP status is " + xmlHttp.status + " instead of 200");
							return;
						};
	
						// Get the row in the response
						var xmlDoc = xmlHttp.responseXML.documentElement;
						var rowNode = xmlDoc.getElementsByTagName("div")[0];
						
						// Copy the row into the current document
						var newRowNode = document.importNode(rowNode, true);
						
						// Add app info to current jsp and update button
						document.getElementById("app_div").appendChild(newRowNode);
						document.getElementById(app_button_id).value = "Hide Application";
					};
	
					// Send XHR request
					xmlHttp.open("GET",url,true);
					xmlHttp.send(null);
				}
				else if (document.getElementById(app_button_id).value == "Show Application") {
					
					// Unhide app info
					var app_div = "app_" + app_button_id;
					document.getElementById(app_div).style.display = "block";	
					document.getElementById(app_button_id).value = "Hide Application";
				}
				else {
					// Hide app info
					var app_div = "app_" + app_button_id;
					document.getElementById(app_div).style.display = "none";
					document.getElementById(app_button_id).value = "Show Application";
				}
			}

		</script>
		<html:base />
		<link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/>
		<title><bean:message key="admin.title" /></title>
	</head>	
	<body>
		<p class="sansserif">
		<div class="container">
		<div class="center_div">
			<table>
			<tr>
				<td><b><bean:message key="admin.applications.title" /></b></td>
				<td></td><td></td><td></td>
				<td><a href="../invalidate.jsp">Logout</a></td>
			</tr>
			</table>
			<br>
			<%
				Vector<String> names = (Vector)session.getAttribute("names");
				Vector<String> gpas = (Vector)session.getAttribute("gpas");
				Vector<String> statuses = (Vector)session.getAttribute("statuses");
				Vector<String> app_id = (Vector)session.getAttribute("app_id");
				
			%>
			<table border="1">
				<tr align="center">
					<th>Name</th>
					<th>Avg Grade</th>
					<th>Admission Status</th>
					<th>Admit</th>
					<th>Reject</th>
					<th>Cancel Decision</th>
					<th>Show Application</th>
				</tr>					
				<logic:iterate id="status" name="statuses" indexId="counter">
					<tr align="center">
						<td><%=names.get(counter) %></td>
						<td><%=gpas.get(counter)%>"</td>
						<td><%=statuses.get(counter) %></td>
						<td>				
							<logic:notEqual name="status" value="Admitted">
								<html:form action="/changeAdmitStatus">
								<html:hidden property="user" value="<%= app_id.get(counter) %>" />
								<html:hidden property="updatedStatus" value="Admitted" />
								<html:submit value="Admit" />
								</html:form>
							</logic:notEqual>
						</td>
						<td>				
							<logic:notEqual name="status" value="Rejected">
								<html:form action="/changeAdmitStatus">
								<html:hidden property="user" value="<%= app_id.get(counter) %>" />
								<html:hidden property="updatedStatus" value="Rejected" />
								<html:submit value="Reject" />
								</html:form>
							</logic:notEqual>
								
						</td>
						<td>				
							<logic:notEqual name="status" value="Pending">
								<html:form action="/changeAdmitStatus">
								<html:hidden property="user" value="<%= app_id.get(counter) %>" />
								<html:hidden property="updatedStatus" value="Pending" />
								<html:submit value="Cancel Decision" />
								</html:form>
							</logic:notEqual>
						</td>
						<td>
						<html:form action="/showapp">
							<html:hidden property="app_id" value="<%= app_id.get(counter)%>" />
							<html:submit property="action"
								value="Get Application" 
								onclick="showApp(this.form); return false"
								styleId="<%=app_id.get(counter)%>"/>
						</html:form>
						</td>
					</tr>
				</logic:iterate>
			</table>
			<br>
			<div id="app_div">
			<%-- -------- App Info will be placed in here -------- --%>
			</div>
		</div>
		</div>
	</body>
</html:html>